package com.spring_mysql_redis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spring_mysql_redis.entity.Message;
import com.spring_mysql_redis.mapper.MessageMapper;
import com.spring_mysql_redis.service.MessageService;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements MessageService {

    @Autowired
    private MessageMapper messageMapper;

    @Override
    public Page<Message> selectMessageList(String toId) {
        Page<Message> messagePage = messageMapper
                .selectPage(new Page<Message>(0, 100), new LambdaQueryWrapper<Message>()
                        .eq(Message::getToId, toId).eq(Message::getState, 20).orderByAsc(Message::getSendTime));

        if(messagePage != null){
            UpdateWrapper<Message> updateWrapper = new UpdateWrapper<>();
            updateWrapper.lambda()
                    .eq(Message::getToId, toId).eq(Message::getState, 20);
            val Message = new Message();
            Message.setState(10);
            //修改后的实体
            int num= messageMapper.update(Message,updateWrapper);
        }
        System.out.println(messagePage);
        return messagePage;
    }
}
